//
//  TheOverviewViewController.m
//  Bees
//
//  Created by NowYouCanSee on 2019/3/11.
//  Copyright © 2019 xiaohan. All rights reserved.
//

#import "TheOverviewViewController.h"
#import "XWalletContentTableCell.h"
#import "UIColor+GradualChanging.h"
#import "MineServer.h"
@interface TheOverviewViewController ()
@property (nonatomic,strong) UILabel *moneyLab;
@property (nonatomic,strong) MineServer *mineServer;

@end

@implementation TheOverviewViewController
//- (instancetype)init{
//    if (self == [super init]) {
//        self = [super initWithStyle:UITableViewStyleGrouped];
//    }
//    return self;
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self.tableView.mj_header beginRefreshing];
}

- (void)loadMore:(BOOL)isMore{
    [self.mineServer getAllValuationIsMore:isMore completion:^(BOOL success, NSString * _Nonnull msg) {
        [self endHeaderFooterRefreshing];
        if (success) {
            if (IS_Property_Hidden) {
                self.moneyLab.text = @"****";
            }else{
                self.moneyLab.text = [NSString stringWithFormat:@"%@ %@",handlerDecimalNumber(self.mineServer.valuationModel.max_valuation,NSRoundPlain, kSymbolLength_EOS).stringValue,self.mineServer.valuationModel.unit];
            }
            [self.tableView reloadData];
        }else{
            [self.view makeToast:msg];
        }
    }];
}

- (void)setupUI{
    self.customNavigationBar.height =  AdaptedWidth(230);
    self.customNavigationBar.backgroundColor =  UIColor.clearColor;
    self.leftBackImg = @"white_back";
    self.isShowLeftBack = YES;
    self.navBarTintColor = UIColor.whiteColor;
//    UIView *headView= [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, AdaptedWidth(208))];
//    [self.customNavigationBar.layer insertSublayer:[UIColor setGradualChangingFrame:CGRectMake(0, 0, kScreenWidth, self.customNavigationBar.height) fromColor:[kGradientColors firstObject] toColor:[kGradientColors lastObject]] atIndex:0];
//    [self.view.layer insertSublayer:[UIColor setGradualChangingFrame:CGRectMake(0, 0, kScreenWidth, self.view.height) fromColor:[kGradientColors firstObject] toColor:[kGradientColors lastObject]] atIndex:0];
    UIImageView *backV = [UIImageView new];
    backV.image = IMAGE_NAMED(@"overviewBack");
    [self.view insertSubview:backV atIndex:0];
    [backV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(AdaptedWidth(60));
        make.top.mas_equalTo(-AdaptedWidth(60));
    }];
//    self.tableView.tableHeaderView = headView;
    self.StatusBarStyle = UIStatusBarStyleLightContent;
    self.moneyLab = [UILabel new];
    self.moneyLab.textColor = UIColorHex(#FFFFFF);
    self.moneyLab.font = kBoldFont(24);
    [self.customNavigationBar addSubview:self.moneyLab];
    [self.moneyLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_offset(0);
        make.bottom.mas_offset(AdaptedWidth(kIsIphoneX?AdaptedWidth(-80):AdaptedWidth(-100)));
    }];
    self.tableView.mj_footer=nil;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.rowHeight = AdaptedWidth(60);
    ViewRadius(self.tableView, AdaptedWidth(13));
    [self.tableView registerClass:UITableViewHeaderFooterView.class forHeaderFooterViewReuseIdentifier:@"headView"];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.mineServer.valuationModel.symbol_list.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.mineServer.valuationModel.symbol_list objectAtIndex:section].symbol_list.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    XWalletContentSubTableCell*cell = [XWalletContentSubTableCell cellForTableView:tableView];
    TokenInfo *token =  [[self.mineServer.valuationModel.symbol_list objectAtIndex:indexPath.section].symbol_list objectAtIndex:indexPath.row];
    [cell.iconV sd_setImageWithURL:[NSURL URLWithString: token.logo]];
    cell.nameLab.text = token.symbol;
    if (IS_Property_Hidden) {
        cell.numLab.text = @"****";
        cell.valueLab.text = @"****";
    }else{
        cell.valueLab.text = [NSString stringWithFormat:@"≈ %@ %@",@"¥",IsNilOrNull(token.valuation)?@"0":handlerDecimalNumber(token.valuation,NSRoundPlain, kSymbolLength_EOS).stringValue];
        cell.numLab.text = token.balance.length?token.balance:@"0.0000";
    }
    return cell;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UITableViewHeaderFooterView *secView = [[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:@"headView"];;
    secView.contentView.backgroundColor = UIColor.whiteColor;
    UILabel *title = [secView.contentView viewWithTag:1001];
    if (!title) {
        title = [UILabel new];
        title.textColor = UIColorHex(#FFAE00);
        title.font = kDefaultFont(13);
        title.tag = 1001;
        [secView.contentView addSubview:title];
        [title mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(kNormalSpace);
            make.centerY.mas_equalTo(0);
        }];
    }
    title.text = [self.mineServer.valuationModel.symbol_list objectAtIndex:section].account;
    return secView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return AdaptedWidth(50);
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 15;
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footV = [UIView new];
    footV.backgroundColor = (self.mineServer.valuationModel.symbol_list.count==section+1)? UIColor.whiteColor:UIColor.groupTableViewBackgroundColor;
    return footV;
}
- (MineServer *)mineServer{
    if (!_mineServer) {
        _mineServer = [MineServer new];
        NSMutableArray *accouts = [NSMutableArray array];
        [[[XHDataBaseManager shareInstance]selectAllLocalWallet]enumerateObjectsUsingBlock:^(Wallet * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [accouts addObject:obj.wallet_name];
        }];
        _mineServer.accounts = [accouts componentsJoinedByString:@","];
    }
    return _mineServer;
}

@end
